//
//  NewFeatureViewController.swift
//  新浪微播
//
//  Created by lxw on 15/12/18.
//  Copyright © 2015年 lxw. All rights reserved.
//

import UIKit
import SnapKit
private let imageCount = 4

private let reuseIdentifier = "Cell"

class NewFeatureViewController: UICollectionViewController {
    init(){
        let layout = UICollectionViewFlowLayout()
        //修改布局
        layout.itemSize = UIScreen.mainScreen().bounds.size
        //修改视图滚动方法
        layout.scrollDirection = UICollectionViewScrollDirection.Horizontal
        //设置item 上下左右的间距
        layout.minimumInteritemSpacing = 0
       layout.minimumLineSpacing = 0
        super.init(collectionViewLayout: layout)
        collectionView?.pagingEnabled = true
        //隐藏滚动条
        collectionView?.showsHorizontalScrollIndicator = false
       //取消弹簧效果
        collectionView?.bounces = false
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
   override func viewDidLoad() {
        super.viewDidLoad()

    self.collectionView!.registerClass(NewFeatureCell.self, forCellWithReuseIdentifier: reuseIdentifier)

        // Do any additional setup after loading the view.
    }
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of items
        return imageCount
    }

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! NewFeatureCell
    
        // 测试代码
        cell.index = indexPath.item
        
        return cell
    }
    
    override func collectionView(collectionView: UICollectionView, didEndDisplayingCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {
        //cell 完成展示之后，indexPath 对应的是 上一个cell indexPath 不能使用indexPath 来获取cell
        print(indexPath.item)
        //获取准确的cell
        let cell = collectionView.visibleCells().last! as!
           NewFeatureCell
        //获取cell 的indexPath
        let index = collectionView.indexPathForCell(cell)!
        
        //判断index.item是不是等于3 执行动画
        if index.item == imageCount - 1{
            //执行动画
            cell.showAnimation()
        }
    }
}

class NewFeatureCell: UICollectionViewCell {
    //添加index属性
    var index: Int = 0 {
        didSet{
            iconView.image = UIImage(named: "new_feature_\(index+1)")
            startBtn.hidden = true
         //   startBtn.hidden = index != 3
           // showAnimation()
        }
    }
    
    private func showAnimation() {
        //动画开始之前，修改transform
        startBtn.transform = CGAffineTransformMakeScale(0, 0)
        startBtn.hidden = false 
        UIView.animateWithDuration(0.5, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 5, options: [], animations: { () -> Void in
            self.startBtn.transform = CGAffineTransformIdentity
            }) { (_) -> Void in
                print("OK")
        }
    }
    @objc private func startBtnDidClick(){
        print(__FUNCTION__)
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        print(frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    private func setupUI() {
        contentView.addSubview(iconView)
        contentView.addSubview(startBtn)
        //使用SnapKit添加约束
        //snp_makeConstraints 表示添加约束
        iconView.snp_makeConstraints { (make) -> Void in
            //make 表示需要添加约束的对象
            make.edges.equalTo(contentView.snp_edges)
        }
        //设置开始按钮的约束
        startBtn.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(contentView.snp_centerX)
            make.bottom.equalTo(contentView.snp_bottom).offset(-180)
        }
        
    startBtn.addTarget(self, action: "startBtnDidClick", forControlEvents: .TouchUpInside)
    }
    
    private lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "new_feature_1"))
    private lazy var startBtn:UIButton = {
        let btn = UIButton()
      btn.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: .Normal)
        
        btn.setBackgroundImage(UIImage (named: "new_feature_finish_button_highlighted"), forState: .Highlighted )
        
        btn.setTitle("开始体验", forState: .Normal)
        btn.setTitleColor(UIColor.whiteColor(), forState: .Normal)
        btn.sizeToFit()
        return btn
    }()
}
